import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    shoppingList: [],
    amount: 0,
  },
  mutations: {
    add(state, data) {
      state.shoppingList.push(data);
      state.amount = state.shoppingList.reduce((sum, cur) => {
        if (typeof cur.price === "number") {
          return sum + cur.price;
        }
        return sum;
      }, 0);
    },
    clear(state) {
      state.shoppingList = [];
    },
  },
  actions: {},
  modules: {},
});
